package com.aly.beanValidate.core.validator.impl;

import com.aly.beanValidate.core.FieldDefinition;
import com.aly.beanValidate.core.GlobalConfigs;
import com.aly.beanValidate.core.validator.CheckValidator;
import com.aly.beanValidate.core.validator.Name;
import com.aly.beanValidate.core.validator.CheckValidators;
import com.aly.beanValidate.i.BeanInfo;
import com.aly.beanValidate.i.FileInfo;
import com.aly.beanValidate.i.FileInfos;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

@Name("Group")
public class GroupValidator extends CheckValidator {
    private List<CheckValidator> validators = new ArrayList<>();

    GroupValidator(Element element, GlobalConfigs globalConfigs) throws Exception {
        super(element, globalConfigs);
        for (Element ele : element.elements()) {
            CheckValidator validatable = CheckValidators.create(ele, globalConfigs);
            validators.add(validatable);
        }
    }

    public GroupValidator(List<CheckValidator> validators) {
        super(null, null);
        this.validators = validators;
    }

    @Override
    public StringBuffer doValidate(FieldDefinition field, Object val, BeanInfo bean, FileInfo fileInfo, FileInfos fileInfos) {
        StringBuffer sb = new StringBuffer();
        for (CheckValidator validator : validators) {
            StringBuffer ret = validator.doValidate(field, val, bean, fileInfo, fileInfos);
            if (null != ret) {
                sb.append(ret);
            }
        }
        return sb;
    }
}
